<template>
  <el-drawer :visible.sync="drawer" direction="rtl" size="50%" :with-header="false" :width="700" append-to-body>
    <div class="drawer_box">
      <div class="drawer_title">
        <div class="drawer_t">
          <div class="drawer_t_l">
            <img class="drawer_t_l_img" src="../../../../../static/skin/herodoo/main/platform/home/img/piliangoumai.png"
              alt="" />
            查看订单
          </div>
          <img class="drawer_t_r" src="../../../../../static/skin/herodoo/main/platform/home/img/guanbi.png" alt=""
            @click="drawer = false" />
        </div>
      </div>
      <div class="drawer_content">
        <div class="tabbar">
          <div @click="tabbar = 0">开票信息</div>
          <div @click="tabbar = 1">开票历史</div>
        </div>
        <block v-if="tabbar == 0">
          <div class="title">订单信息</div>
          <div class="content-box">
            <div class="label">订单编号：</div>
            <div class="desc">{{ messageList.PayCode }}</div>
          </div>
          <div class="content-box">
            <div class="label">权益名称：</div>
            <div class="desc name" @click="handleShowDetail">
              {{ messageList.Name }}
            </div>
          </div>
          <div class="content-box">
            <div class="label">订单购买数量：</div>
            <div class="desc">{{ messageList.Quantity }}</div>
          </div>
          <div class="content-box">
            <div class="label">订单支付总金额(元)：</div>
            <div class="desc">{{ messageList.UserPayTotalPrice }}</div>
          </div>
          <div class="content-box">
            <div class="label">卖家：</div>
            <div class="desc">{{ messageList.CompanyIDName }}</div>
          </div>
          <div class="content-box">
            <div class="label">购买时间：</div>
            <div class="desc">{{ messageList.PayTime }}</div>
          </div>
          <div v-if="messageList.StatusID != 1">
            <div class="title">发票信息</div>
            <div class="content-box">
              <div class="label">单位名称：</div>
              <div class="desc">{{ messageList.CompanyName }}</div>
            </div>
            <div class="content-box">
              <div class="label">税号：</div>
              <div class="desc">{{ messageList.TaxCode }}</div>
            </div>
            <div class="content-box">
              <div class="label">单位地址：</div>
              <div class="desc">{{ messageList.CompanyAddress }}</div>
            </div>
            <div class="content-box">
              <div class="label">单位电话：</div>
              <div class="desc">{{ messageList.Phone }}</div>
            </div>
            <div class="content-box">
              <div class="label">开户银行：</div>
              <div class="desc">{{ messageList.BankName }}</div>
            </div>
            <div class="content-box">
              <div class="label">银行账号：</div>
              <div class="desc">{{ messageList.BankAccount }}</div>
            </div>

            <div class="title">开票信息</div>
            <div class="content-box">
              <div class="label">申请开票金额(元)：</div>
              <div class="desc">{{ messageList.Amount }}</div>
            </div>
            <div class="content-box">
              <div class="label">申请开票时间：</div>
              <div class="desc">{{ messageList.CreateTime }}</div>
            </div>
            <div class="content-box" v-if="messageList.Status == 4">
              <div class="label">撤销发票时间：</div>
              <div class="desc">{{ messageList.ReopenTime }}</div>
            </div>
          </div>
          <!-- <div class="title">{{ messageList.StatusID == 1 ? '已核销列表':'申请开票列表' }}</div>
          <el-table :data="concelList" stripe max-height="250" style="width: 100%; font-size: 1rem">
            <el-table-column prop="VerificeCode" label="核销码"></el-table-column>
            <el-table-column prop="UserPayTotalPrice" label="核销金额(元)"></el-table-column>
            <el-table-column prop="VerificeTime" label="核销时间"></el-table-column>
          </el-table>
          <div class="end" v-if="total > 0">
            <el-pagination background layout="prev, pager, next" :total="total" :current-page="currentPage"
              :page-size="pageSize" @current-change="handleCurrentChange">
            </el-pagination>
          </div> -->

          <!-- 开票文件 -->
          <div class="content-box col" v-if="messageList.Status == 3">
            <div class="label">发票</div>
            <div class="desc">
              <div class="file-item" v-for="(item, index) in FileList" :key="index">
                <div class="file-action" @click="filePreview(item, index)">
                  预览
                </div>
                <div class="file-name" @click="filePreview(item, index)">
                  {{ item.name }}
                </div>
                <div>{{ (item.size / 1024 / 1024 || 0).toFixed(2) }}MB</div>
              </div>
            </div>
          </div>
          <div class="title">发票</div>
          <div class="content-box">
            <div class="file-item" v-for="(item, index) in getArr(messageList.FilePath)" :key="index">
              <div class="file-name" @click="filePreview(item, index)">
                {{ item.name }}
              </div>
              <div>{{ (item.size / 1024 / 1024 || 0).toFixed(2) }}MB</div>
            </div>
          </div>
        </block>
        <block v-if="tabbar == 1">
          <div v-for="item in invoiceList" :key="item.ID" class="box">
            <div class="title">发票信息</div>
            <div class="content-box">
              <div class="label">单位名称：</div>
              <div class="desc">{{ item.CompanyName }}</div>
            </div>
            <div class="content-box">
              <div class="label">税号：</div>
              <div class="desc">{{ item.TaxCode }}</div>
            </div>
            <div class="content-box">
              <div class="label">单位地址：</div>
              <div class="desc">{{ item.CompanyAddress }}</div>
            </div>
            <div class="content-box">
              <div class="label">单位电话：</div>
              <div class="desc">{{ item.Phone }}</div>
            </div>
            <div class="content-box">
              <div class="label">开户银行：</div>
              <div class="desc">{{ item.BankName }}</div>
            </div>
            <div class="content-box">
              <div class="label">银行账户：</div>
              <div class="desc">{{ item.BankAccount }}</div>
            </div>
            <div class="title">开票信息</div>
            <div class="content-box">
              <div class="label">申请开票总金额（元）：</div>
              <div class="desc">{{ item.Amount }}</div>
            </div>
            <div class="content-box">
              <div class="label">申请开票时间：</div>
              <div class="desc">{{ item.CreateTime }}</div>
            </div>
            <div class="content-box" v-if="item.BillingTime != ''">
              <div class="label">开票时间：</div>
              <div class="desc">{{ item.BillingTime }}</div>
            </div>
            <div class="title">发票</div>
            <div class="content-box col" v-if="getArr(item.FilePath).length > 0">
              <div class="file-item" v-for="(item, index) in getArr(item.FilePath)" :key="index">
                <div class="file-name" @click="filePreview(item, index)">
                  {{ item.name }}
                </div>
                <div>{{ (item.size / 1024 / 1024 || 0).toFixed(2) }}MB</div>
              </div>
            </div>
          </div>
        </block>
      </div>

      <div class="drawer_footer">
        <button class="footer-btn" @click="handleShowMake" v-if="messageList.StatusID == 1">
          申请开票
        </button>
        <button class="footer-btn" @click="handleRevocation"
          v-if="messageList.StatusID == 2 && invoiceList.length == 1">
          撤销
        </button>
        <button class="footer-btn" @click="handleShowMake" v-if="messageList.StatusID == 3 && getAllStatus()">
          申请换开
        </button>
        <button class="footer-btn" @click="handleShowMake" v-if="messageList.StatusID == 4 && invoiceList.length == 1">
          申请重开
        </button>
      </div>
    </div>
    <!-- 申请开票弹窗 -->
    <apply-invoice :octopus="octopus" :messageList="messageList" ref="applyInvoice" @drawerUpDetail="handleShowDetail"
      @refresh="drawer = false, $emit('refresh')"></apply-invoice>
    <!-- 预览文件 -->
    <el-drawer :visible.sync="fileDrawer" direction="rtl" size="50%" :with-header="false" :width="700" append-to-body>
      <embed :type="fileType" :src="fileSrc" style="width: 100%; height: 100%;" />
    </el-drawer>
    <!-- 查看权益弹窗 -->
    <rights-detail :octopus="octopus" ref="rightsDetail"></rights-detail>
  </el-drawer>
</template>

<script>
import ApplyInvoice from "./ApplyInvoice.vue";
import rightsDetail from "./rightsDetail.vue";

export default {
  components: { ApplyInvoice, rightsDetail },
  props: {
    messageList: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      octopus: this.$parent.octopus,
      drawer: false,
      concelList: [],
      total: 0,
      currentPage: 1, //当前页
      pageSize: 10, // 每页显示条数
      fileDrawer: false,
      invoiceList: [],
      tabbar: 0
    };
  },
  watch: {
    drawer(v) {
      this.tabbar = 0;
      v && this.getCancelList();
    }
  },
  computed: {
    FileList() {
      try {
        return JSON.parse(this.messageList.FilePath);
      } catch (e) {
        return [];
      }
    }
  },
  methods: {
    getArr(str) {
      let array = [];
      array = str ? JSON.parse(str) : [];
      return array;
    },
    drawerOp() {
      this.drawer = true;
    },
    // 核销列表
    getCancelList() {
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 1264,
          cellData: [
            { name: "ID" }, // ID
            { name: "UserPayTotalPrice" }, // 核销金额(元)
            { name: "VerificeCode" }, // 核销码
            { name: "VerificeTime" } // 核销时间
          ],
          paramData: {
            Code: this.messageList.PayCode,
            PurchaseID: this.messageList.ID
          },
          pageData: {
            index: this.currentPage,
            rowSize: this.pageSize
          }
        },
        data => {
          if (data.status.code == 200) {
            this.total = data.content.pageData.totalRows;
            this.concelList = data.content.queryData;
            this.getHist();
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    handleCancelChange(val) {
      this.currentPage = val;
      this.getCancelList();
    },
    // 显示申请开票弹窗
    handleShowMake() {
      this.$refs.applyInvoice.drawerOp();
    },
    // 撤销开票
    handleRevocation() {
      this.$confirm("该操作只允许撤销一次，是否确认撤销开票?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.octopus.request.api.post(
          "/Octopus/Engine/Item",
          {
            id: 1211,
            type: "UPDATE",
            value: this.messageList.InvoiceID,
            cellData: [
              {
                name: "ReopenTime",
                defaultValue:
                  new Date().toJSON().substring(0, 10) +
                  " " +
                  new Date().toTimeString().substring(0, 8)
              }, // 是否重开 （0 正常，1 重开）
              { name: "IsDelete", defaultValue: 1 } // 是否重开 （0 正常，1 重开）
            ]
          },
          data => {
            if (data.status.code == 200) {
              this.octopus.tips.success("发票申请已撤销");
              this.drawer = false;
              this.$emit("refresh");
            } else {
              this.octopus.tips.error(data);
            }
          }
        );
      });
    },
    handleShowDetail() {
      this.$refs.rightsDetail.init(
        this.messageList.EquityID,
        this.messageList.CompanyID
      );
      this.$refs.rightsDetail.drawerOp();
    },
    // 开票历史
    getHist() {
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 1394,
          cellData: [
            { name: "Amount" }, // Amount
            { name: "BankAccount" }, // BankAccount
            { name: "BankName" }, // BankName
            { name: "BillingTime" }, // BillingTime
            { name: "CompanyAddress" }, // CompanyAddress
            { name: "CompanyName" }, // CompanyName
            { name: "CreateTime" }, // CreateTime
            { name: "FilePath" }, // FilePath
            { name: "ID" }, // ID
            { name: "IsDelete" }, // IsDelete
            { name: "Phone" }, // Phone
            { name: "Remarks" }, // Remarks
            { name: "ReopenTime" }, // ReopenTime
            { name: "TaxCode" }, // TaxCode
            { name: "TypeID" } // TypeID
          ],
          paramData: {
            PurchaseID: this.messageList.ID
          },
          pageData: {}
        },
        data => {
          if (data.status.code == 200) {
            this.invoiceList = data.content.queryData;
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    // 预览文件
    filePreview(item, index) {
      let file_path = item.url.substring(item.url.indexOf("upload") + 6);
      let view_path =
        this.octopus.config.url +
        "/Octopus/Engine/Viewer/" +
        item.name +
        "?" +
        "path=" +
        file_path +
        "&access_token=" +
        this.octopus.passport.token;

      let arr = item.name.split(".");
      let type = arr[arr.length - 1];
      this.fileType = `application/${type}`;
      this.fileSrc = view_path;
      this.fileDrawer = true;
    },
    getAllStatus() {
      let find = this.invoiceList.filter(x => x.FilePath && x.FilePath != "");
      return find.length >= 2 ? false : true;
    }
  }
};
</script>
<style scoped>
.drawer_box {
  padding: 3rem 0.8rem 3rem 0;
  height: calc(100% - 5rem);
  display: flex;
  flex-direction: column;
}

.drawer_title {
  box-shadow: 0px 5px 5px rgb(230 228 228 / 30%);
  padding-bottom: 1rem;
}

.drawer_t {
  padding: 0 2.8rem;
  display: flex;
  justify-content: space-between;
}

.drawer_t_l {
  display: flex;
  align-items: center;
  font-size: 2rem;
  font-weight: bold;
}

.drawer_t_l_img {
  width: 2.8rem;
  margin-right: 1rem;
}

.drawer_t_r {
  width: 3rem;
  height: 3rem;
  cursor: pointer;
}

.button_box {
  margin-top: 1rem;
  padding: 0 3rem;
  display: flex;
}

.drawer_content {
  overflow-y: auto;
  margin-top: 1.5rem;
  height: calc(100vh - 11rem);
  padding: 0 3rem;
  width: 100%;
  padding-bottom: 1rem;
}

.drawer_content .title {
  font-size: 1.8rem;
  font-weight: bold;
}

.drawer_content .driver-title {
  margin-top: 1em;
}

.full-line {
  margin: 2rem auto;
  width: 95%;
  height: 1px;
  background-color: #f78c51;
}

.content-box {
  display: flex;
  align-items: center;
  margin: 2rem 0 2rem 2em;
}

.col {
  flex-direction: column;
  align-items: flex-start;
}

.mt-10 {
  margin-top: 10rem;
}

.content-box .label {
  width: 25%;
  font-size: 1.6rem;
}

.content-box .desc {
  flex: 1;
  font-size: 1.6rem;
}

.name {
  color: #288ffd;
  text-decoration: underline;
  cursor: pointer;
}

.content-box .desc .desc-img {
  width: 14rem;
  height: 14rem;
  border-radius: 1rem;
  display: inline-block;
  margin-right: 1rem;
  background-color: aqua;
}

.drawer_footer {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  margin-right: 30px;
}

.drawer_footer .footer-btn {
  line-height: 4rem;
  background-color: #f78c51;
  color: #fff;
  margin: 0 1rem;
  padding: 0 3rem;
  border-radius: 0.5rem;
  cursor: pointer;
  border: none;
  font-size: 1.6rem;
}

.end {
  display: flex;
  justify-content: flex-end;
}

.file-item {
  height: 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(0, 0, 0, 0.3);
}

.file-container .file-item .file-action {
  cursor: pointer;
}

.file-container .file-item .file-name {
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #288ffd;
  text-decoration: underline;
  cursor: pointer;
}

>>>.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #ff8a36;
}

.tabbar {
  display: flex;
  flex-direction: row;
  margin-bottom: 20px;
}

.tabbar div {
  padding: 10px 30px;
  cursor: pointer;
  border: 1px solid #a6a6a6;
}

.tabbar div:nth-child(1) {
  border-radius: 5px 0 0 5px;
}

.tabbar div:nth-child(2) {
  border-radius: 0 5px 5px 0;
}

.file-item {
  display: flex;
  flex-direction: row;
  color: #288ffd;
  cursor: pointer;
}

.box {
  border: 1px solid #000;
  padding: 20px;
  margin-bottom: 20px;
}
</style>
